<?php

namespace App\Http\Controllers;

use App\Models\PartCompany;
use App\Services\CommonService;
use App\Services\PartCompanyService;
use App\Services\PhotoClassifyService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PartCompanyController extends Controller
{
    public $partCompanyService;

    public function __construct(PartCompanyService $partCompanyService)
    {
        parent::__construct();
        $this->partCompanyService = $partCompanyService;
    }

    /**
     * 立卷阶段 参建单位 数据列表
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:47
     */
    public function index(Request $request): JsonResponse
    {
        $projectId  = $request->input('project_id');
        $searchType = $request->input('search_type', 0);
        $perPage    = $request->input('per_page') ?: $this->limit;

        $list = $this->partCompanyService->getList($projectId, $searchType, $perPage);

        return $this->success($list);
    }

    /**
     * 刷新数据
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024-10-19 17:01
     */
    public function refreshData(Request $request): JsonResponse
    {
        $projectId = $request->input('project_id');

        $this->partCompanyService->refreshData($projectId);

        return $this->success('', '统计数据已更新！');
    }

    /**
     * 获取参建单位信息
     *
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Exceptions\ApiException
     * @author: lxk
     * @date: 2024/3/20 9:47
     */
    public function show(Request $request) : JsonResponse
    {
        $id = $request->input('id');
        $info = $this->partCompanyService->getInfo($id);

        return $this->success($info);
    }

    /**
     * 添加 或 编辑
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:47
     */
    public function store(Request $request) : JsonResponse
    {
        $msg = request()->filled('id') ? '修改' : '添加';

        if (!$this->partCompanyService->saveData($request->all())) {
            return $this->error($msg . '失败！');
        }

        return $this->success('', $msg . '成功！');
    }

    /**
     * 检索阶段参见单位
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:48
     */
    public function getList(Request $request) : JsonResponse
    {
        $classifyId = $request->input('classify_id', 0);

        $list = $this->partCompanyService->getPartCompanies($classifyId);

        return $this->success($list);
    }

    /**
     * 获取一条项目分类目录的详情
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:48
     */
    public function classifiesInfo(Request $request) : JsonResponse
    {
        $info = $this->partCompanyService->getProjectClassifiesInfo($request->input('id', 0));

        return $this->success($info);
    }

    /**
     * 匹配施工单位
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:48
     */
    public function matchConstructUnit(Request $request) : JsonResponse
    {
        $supervisorId = $request->input('supervisor_id'); //获取监理单位id
        $constructIds = $request->input('construct_ids'); //获取施工单位ids

        if (!$this->partCompanyService->bindConstructUnits($supervisorId, $constructIds)) {
            return $this->error('匹配失败！');
        }

        return $this->success('', '匹配成功!');
    }

    /**
     * 获取项目下的施工单位
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:49
     */
    public function getConstructUnits(Request $request) : JsonResponse
    {
        $projectId    = $request->input('project_id');
        $supervisorId = $request->input('supervisor_id'); //监理单位id

        $list = $this->partCompanyService->constructUnits($projectId, $supervisorId);

        return $this->success($list);
    }

    /**
     * 回收站列表（进行软删除的数据）
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:49
     */
    public function recycle(Request $request) : JsonResponse
    {
        $data = $this->partCompanyService->getRecycles($request);

        return $this->success($data);
    }

    /**
     * 单个软删除（软删除相关的全部是Laravel内置操作方法）
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:49
     */
    public function destroy(Request $request) : JsonResponse
    {
        $id = $request->input('id');
        if (!PartCompany::query()->where('id', $id)->delete()) {
            return $this->error('删除失败！');
        };
        CommonService::addActionLog('单个软删除参建单位', "数据ID:{$id}");

        return $this->success('', '删除成功');
    }

    /**
     * 批量软删除，ids可以是以","逗号分割的字符串，也可以是数组形式
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:50
     */
    public function destroyMore(Request $request) : JsonResponse
    {
        $ids = to_array($request->input('ids'));
        if (!PartCompany::destroy($ids)) {
            return $this->error('删除失败！');
        };
        $ids = implode(',', $ids);
        CommonService::addActionLog('批量软删除参建单位', "数据ID:{$ids}");

        return $this->success('', '放入回收站成功');
    }

    /**
     * 恢复批量数据
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:50
     */
    public function retrieveMore(Request $request) : JsonResponse
    {
        $ids = to_array($request->input('ids'));
        if (!PartCompany::query()->whereIn('id', $ids)->restore()) {
            return $this->error('恢复失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog('恢复批量参建单位', "数据ID:{$ids}");

        return $this->success('', '恢复成功');
    }

    /**
     * 永久删除批量数据，ids可以是以","逗号分割的字符串，也可以是数组形式
     *
     * @param Request $request
     * @return JsonResponse
     * @author: lxk
     * @date: 2024/3/20 9:51
     */
    public function deleteMore(Request $request) : JsonResponse
    {
        $ids = to_array($request->input('ids'));
        if (!PartCompany::withTrashed()->whereIn('id', $ids)->forceDelete()) {
            return $this->error('删除失败！');
        }
        $ids = implode(',', $ids);
        CommonService::addActionLog('永久删除批量参建单位', "数据ID:{$ids}");

        return $this->success('', '删除成功');
    }

}
